package com.mini_colombia.descargas;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.mini_colombia.R;
import com.mini_colombia.auxiliares.Ringtone;

public class DescargasRingtones extends Activity implements MediaPlayer.OnPreparedListener
{
	ArrayList<String> canciones = new ArrayList<String>();
	
	private SeekBar seekBarActual;
	
	private MediaPlayer playerActual;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_descargas_ringtones);
		new Ringtone((LinearLayout)findViewById(R.id.linearLayoutRingtones),"https://s3.amazonaws.com/mini-cooper/descargas/ringtones/1-04+A+Letter+To+Jimmy+Page.mp3",this);
		new Ringtone((LinearLayout)findViewById(R.id.linearLayoutRingtones),"https://s3.amazonaws.com/mini-cooper/descargas/ringtones/04+One.mp3",this);
//		canciones.add("https://s3.amazonaws.com/mini-cooper/descargas/ringtones/04+One.mp3");
//		canciones.add("https://s3.amazonaws.com/mini-cooper/descargas/ringtones/1-04+A+Letter+To+Jimmy+Page.mp3");
//		seekBarActual = null;
//
//		LinearLayout layoutPrincipal = (LinearLayout)findViewById(R.id.linearLayoutRingtones);
//
//		LinearLayout layoutModulo;
//		LinearLayout layoutMargenes;
//		for(int i=0; i<2; i++)
//		{
//			final MediaPlayer player = new MediaPlayer();
//			player.setAudioStreamType(AudioManager.STREAM_MUSIC);
//			try 
//			{
//				player.setDataSource(canciones.get(i));
//				player.setOnPreparedListener(this);
//			} 
//			catch (IllegalArgumentException e) 
//			{
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} 
//			catch (IllegalStateException e) 
//			{
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} 
//			catch (IOException e) 
//			{
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} 
//
//			layoutModulo = new LinearLayout(this);
//			layoutModulo.setBackgroundResource(R.drawable.fondo_ringtones);
//			layoutModulo.setOrientation(LinearLayout.VERTICAL);
//
//			layoutMargenes = new LinearLayout(this);
//			LinearLayout.LayoutParams paramsLayoutMargenes = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//			paramsLayoutMargenes.setMargins(20, 10, 20, 0);
//			layoutMargenes.setLayoutParams(paramsLayoutMargenes);
//			layoutMargenes.setOrientation(LinearLayout.VERTICAL);
//
//
//
//			RelativeLayout relSuperior = new RelativeLayout(this);
//			RelativeLayout.LayoutParams relSupParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
//			relSuperior.setLayoutParams(relSupParams);
//
//			TextView textoRingtone = new TextView(this);
//			textoRingtone.setText("Ringtone 1");
//			textoRingtone.setTextColor(Color.WHITE);
//			RelativeLayout.LayoutParams paramsTRingtone = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//			paramsTRingtone.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
//			relSuperior.addView(textoRingtone,paramsTRingtone);
//
//			//Creacion del sekkbar
//			 SeekBar sb = new SeekBar(this);
//			
//			ImageButton botonPlay = new ImageButton(this);
//			botonPlay.setImageResource(R.drawable.play);
//			botonPlay.setBackgroundColor(Color.TRANSPARENT);
//			RelativeLayout.LayoutParams paramsBPlay = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//			paramsBPlay.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
//			botonPlay.setId(i+1);
////			botonPlay.setOnClickListener(new OnClickListener() 
////			{
////
////				@Override
////				public void onClick(View v) 
////				{
////					if(player.getCurrentPosition()>0)
////					{
////						player.start();
////					}
////					else
////					{
////						player.prepareAsync();
////						seekBarActual=sb;
////					}
////
////				}
////			});
//			relSuperior.addView(botonPlay,paramsBPlay);			
//
//			ImageButton botonPause = new ImageButton(this);
//			botonPause.setImageResource(R.drawable.pause);
//			botonPause.setBackgroundColor(Color.TRANSPARENT);
//			RelativeLayout.LayoutParams paramsBPause = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
//			paramsBPause.addRule(RelativeLayout.LEFT_OF, botonPlay.getId());
//			paramsBPause.setMargins(0, 0, 25, 0);
////			botonPause.setOnClickListener(new OnClickListener() {
////				
////				@Override
////				public void onClick(View v) 
////				{
////					player.stop();
////				}
////			});
//			relSuperior.addView(botonPause,paramsBPause);
//
//
//			layoutMargenes.addView(relSuperior);
//
//			sb.setThumb(getResources().getDrawable(R.drawable.seek_thumb));	
//			sb.setProgressDrawable(getResources().getDrawable(R.drawable.seekbar_progress_bg));
//			layoutMargenes.addView(sb);
//
//			RelativeLayout relInferior = new RelativeLayout(this);
//			RelativeLayout.LayoutParams r = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//			r.setMargins(0, 15, 0, 0);
//			relInferior.setLayoutParams(r);
//
//			Button botonPonerRingtone = new Button(this);
//			botonPonerRingtone.setBackgroundResource(R.drawable.poner_ringtone);
//			RelativeLayout.LayoutParams paramsBPRingtone = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
//			paramsBPRingtone.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
//			relInferior.addView(botonPonerRingtone, paramsBPRingtone);
//
//			Button botonDescargar = new Button(this);
//			botonDescargar.setBackgroundResource(R.drawable.descargar_ringtone);
//			RelativeLayout.LayoutParams paramsBDescargar = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
//			paramsBDescargar.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
//			relInferior.addView(botonDescargar, paramsBDescargar);
//
//			layoutMargenes.addView(relInferior);
//
//			layoutModulo.addView(layoutMargenes);
//			layoutPrincipal.addView(layoutModulo);
//
////			Ringtone r1 = new Ringtone(botonPlay, botonPause, sb, player);
//
//		}
	}

	@Override
	public void onBackPressed() 
	{
		super.onBackPressed();
	}

	@Override
	public void onPrepared(MediaPlayer mp) 
	{
		playerActual = mp;
		int duracion = mp.getDuration();
		darSeekBarActual().setProgress(0);
		darSeekBarActual().setMax(duracion);
		mp.start();
		new Player().execute(duracion);
	}
	
	private class Player extends AsyncTask<Integer, Integer, Void>
	{
		SeekBar seekBar;
		
		MediaPlayer playerActual;
		
		int progreso;
		
		@Override
		protected void onPreExecute() 
		{
			seekBar= darSeekBarActual();
			playerActual = darPlayerActual();
			progreso=0;
		}
		
		
		@Override
		protected Void doInBackground(Integer... params) 
		{
			int duracion = params[0];
			seekBar.setMax(duracion);
			while(progreso<duracion)
			{
				progreso = playerActual.getCurrentPosition();
				publishProgress(progreso);
			}
			return null;
		}
		
		@Override
		protected void onProgressUpdate(Integer... values) 
		{
			seekBar.setProgress(values[0]);
		}
		
		@Override
		protected void onPostExecute(Void result) 
		{
			playerActual.seekTo(0);
			playerActual.stop();
			seekBar.setProgress(0);
		}
		
	}
	
	public SeekBar darSeekBarActual()
	{
		return seekBarActual;
	}
	
	public MediaPlayer darPlayerActual()
	{
		return playerActual;
	}
}
